
//ajax配置

import axios from 'axios';
import router from '../router/index.js'
import { Loading, Message } from 'element-ui'


let http = {

};

http.api = axios.create();

http.api.defaults.timeout = 100000;
// http.api.defaults.baseURL = 'http://39.108.248.183:8762/' ;
http.api.defaults.baseURL =process.env.BASE_APII
let s ;
http.api.interceptors.request.use(

  config => {

    //在所有请求头部添加token值
    let token = localStorage.token;
    if (token) {
    	config.headers.token = token;
    }
    return config
  },
  error => {
    closeLoading();
    return Promise.reject(error)
  }
  );

http.api.interceptors.response.use(
	response=>{
	  closeLoading();
		return response
	},
	error =>{
    closeLoading();
    if(error.response){
			switch(error.response.status){
				//返回登录页
        case 400:
          Message.error(error.response.data.msg);
          break;
				case 401 :
				router.push('/login');
				break;
				case 403:
				break;

			}
		}
		return Promise.reject(error.response.data);
	}
	)

function  startLoading(){
  s = Loading.service({
    lock: false,
    text: '加载中...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
}

function closeLoading(){
  if(s){
    s.close();
  }
}
export default http;
